package com.project.smarthome.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.project.smarthome.SmartApplication;
import com.project.smarthome.common.view.StatusBarUtil;
import com.sds.smarthome.foundation.util.XLog;


public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener, UIView {

    private ProgressDialog mLoadingDialog;

    protected abstract void initVariable();

    protected abstract void initView();

    protected abstract void loadData();

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (!this.isTaskRoot()) {
            //判断该Activity是不是任务空间的源Activity，“非”也就是说是被系统重新实例化出来
            // 如果你就放在launcher Activity中话，这里可以直接return了
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
                // finish()之后该活动会继续执行后面的代码，你可以logCat验证，加return避免可能的exception
            }
        }
        SmartApplication.getContext().addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mLoadingDialog = new ProgressDialog(this);
        mLoadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        initVariable();
        initView();
        loadData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ViewNavigator.setCurContext(this);//for nav
        setStatusBar();
        setTitleBarColor();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        XLog.e("activity onRestart");
    }

    protected void setStatusBar() {
        StatusBarUtil.setTransparent(this);
    }

    protected void setTitleBarColor() {
    }

    public void showLoading(String msg) {
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCanceledOnTouchOutside(true);
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public void showNoCanceledLoading(String msg) {
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        if (mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public void hideLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public void showAdminDialog() {
//        SosDialog sosDialog = new SosDialog(this);
//        sosDialog.getDialog(this, "请联系管理员", "确定");
    }

    public void showAdminDialog(String msg, String btn) {
//        SosDialog sosDialog = new SosDialog(this);
//        sosDialog.getDialog(this, msg, btn);
    }

    protected void showToast(int resId) {
        Toast toast = Toast.makeText(this, getResources().getString(resId), Toast.LENGTH_SHORT);
        toast.show();
    }

    public void showToast(String msg) {
        Toast toast = Toast.makeText(this, msg, 2*1000);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void showLongToast(String msg) {
        Toast toast = Toast.makeText(this, msg, 5*1000);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    protected void startActivity(Context fromClass, Class<?> toClass) {
        startActivity(fromClass, toClass, null);
    }

    public void startActivityForResult(Activity fromClass, Class<?> toClass, int requestCode) {
        startActivityForResult(fromClass, toClass, requestCode, null);
    }

    public void startActivityForResult(Activity fromClass, Class<?> toClass, int requestCode, Bundle data) {
        Intent intent = new Intent();
        intent.setClass(fromClass, toClass);
        if (null != data) {
            intent.putExtras(data);
        }
        fromClass.startActivityForResult(intent, requestCode);
    }

    protected void startActivity(Context fromClass, Class<?> toClass, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(fromClass, toClass);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        fromClass.startActivity(intent);
    }

    public void showCodelibUploadImage(boolean show) {
//        findViewById(R.id.img_code_upload).setVisibility(show ? View.VISIBLE : View.GONE);
    }

    protected ListView initTitle(String title) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
        return null;
    }

    protected void initTitle(String title, int imgSource) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(imgSource);
//        txtTitle.setOnClickListener(this);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        findViewById(R.id.txt_right).setVisibility(View.GONE);
//        findViewById(R.id.img_action_right).setVisibility(View.GONE);
//        findViewById(R.id.img_code_upload).setVisibility(View.GONE);
    }

    protected void initTitle(String title, int imgSource, int imgRight) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(imgSource);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.VISIBLE);
//        imgActionRight.setImageResource(imgRight);
//        findViewById(R.id.txt_right).setVisibility(View.GONE);
//        imgActionRight.setOnClickListener(this);
    }


    protected void initTitle(String title, int imgSource, String right) {
//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(imgSource);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//        txtRight.setVisibility(View.VISIBLE);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.GONE);
//        findViewById(R.id.img_action_right).setVisibility(View.GONE);
//        findViewById(R.id.img_code_upload).setVisibility(View.GONE);
    }

    protected void initTitle(String title, int imgSource, int imgSource2, String right) {

//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//
//        ImageView imgLeft = (ImageView) findViewById(R.id.img_action_left);
//        imgLeft.setImageResource(imgSource);
//        findViewById(R.id.img_action_left).setOnClickListener(this);
//
//        ImageView imgCode = (ImageView) findViewById(R.id.img_code_upload);
//        imgCode.setImageResource(imgSource2);
//        findViewById(R.id.img_code_upload).setOnClickListener(this);
//        imgCode.setVisibility(View.VISIBLE);
//
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//        txtRight.setVisibility(View.VISIBLE);
//
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.GONE);
    }

    protected void initNoLeftTitle(String title, String right) {

//        TextView txtTitle = (TextView) findViewById(R.id.txt_action_title);
//        txtTitle.setText(title);
//        TextView txtRight = (TextView) findViewById(R.id.txt_right);
//        txtRight.setText(right);
//        txtRight.setOnClickListener(this);
//        txtRight.setVisibility(View.VISIBLE);
//        ImageView imgActionRight = (ImageView) findViewById(R.id.img_action_right);
//        imgActionRight.setVisibility(View.GONE);
//        findViewById(R.id.img_action_left).setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
//        if (v.getId() == R.id.img_action_left) {
//            findViewById(R.id.img_action_left).setClickable(false);
//            finish();
//        }
    }

    public void exit() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoadingDialog = null;
        SmartApplication.getContext().removeActivity(this);
        XLog.d("onDestroy: " + this);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        XLog.i("gc for activity:" + this);
    }


}
